package com.aube.mis.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.aube.api.aubepro.video.VideoInfoVoService;
import com.aube.api.cc.CcVoService;
import com.aube.api.service.GoodsVoService;
import com.aube.api.v2.service.actor.ActorVoV2Service;
import com.aube.api.v2.service.businessModel.BusinessModelVoV2Service;
import com.aube.api.v2.service.source.SourceRelatedVoV2Service;
import com.aube.api.v2.service.stagephoto.StagePhotoVoV2Service;
import com.aube.api.v2.service.video.VideoVoV2Service;
import com.aube.api.v2.vo.actor.req.ActorQueryReqVo;
import com.aube.api.v2.vo.actor.resp.ActorVo;
import com.aube.api.v2.vo.stagephoto.req.StagePhotoReqVo;
import com.aube.api.v2.vo.stagephoto.resp.StagePhotoRespVo;
import com.aube.api.v2.vo.video.req.VideoQueryReqVo;
import com.aube.api.v2.vo.video.resp.VideoVo;
import com.aube.apiv2.service.prize.AubePrizeV2VoService;
import com.aube.apiv2.vo.prize.AubePrizeV2Vo;
import com.aube.common.req.vo.PaginationReqVo;
import com.aube.common.resp.vo.PaginationRespVo;
import com.aube.mis.dto.ActorDTO;
import com.aube.mis.dto.ItemDTO;
import com.aube.mis.dto.PaginationResultDTO;
import com.aube.mis.dto.PicDTO;
import com.aube.mis.dto.PrizeDTO;
import com.aube.mis.dto.VideoDTO;
import com.aube.mis.enums.CcStatus;
import com.aube.mis.service.AbstractService;
import com.aube.mis.service.ResourceService;
import com.aube.mis.vo.DataTableCriterias;
import com.aube.support.ResultCode;
import com.aube.vo.GoodsVoResp;
import com.aube.vo.resp.video.VideoDefinitionsVo;
import com.aube.vo.resp.video.VideoPlayVo;

@Service
public class ResourceServiceImpl extends AbstractService implements ResourceService {

	private static Logger log = LoggerFactory.getLogger(ResourceServiceImpl.class);

	@Resource
	VideoVoV2Service videoRemoteService;

	@Resource
	ActorVoV2Service actorRemoteService;

	@Resource
	StagePhotoVoV2Service stagePhotoRemoteService;

	@Resource
	VideoInfoVoService videoInfoRemoteService;

	@Resource
	CcVoService ccRemoteService;

	@Resource
	SourceRelatedVoV2Service sourceRelatedVoV2Service;

	@Resource
	BusinessModelVoV2Service businessModelService;

	@Resource
	GoodsVoService goodsVoService;
	
	@Resource
	AubePrizeV2VoService prizeV2VoService;

	@Override
	@SuppressWarnings("unchecked")
	public PaginationResultDTO<VideoDTO> getVideoList(DataTableCriterias criterias, String showId) {
		PaginationReqVo<VideoQueryReqVo> reqVo = fillPaginationInfo(criterias);
		reqVo.setShowid(showId);
		ResultCode<PaginationRespVo<VideoVo>> serverResp = videoRemoteService.queryList(reqVo);
		List<VideoDTO> resultList = (List<VideoDTO>) CollectionUtils.collect(serverResp.getRetval().getResp(), new Transformer() {

			@Override
			public VideoDTO transform(Object object) {
				return convertVideoDTO((VideoVo) object);
			}
		});
		PaginationResultDTO<VideoDTO> result = new PaginationResultDTO<VideoDTO>();
		result.setTotal(serverResp.getRetval().getTotal());
		result.setResultList(resultList);
		return result;
	}

	@Override
	public VideoDTO getVideoDetail(String showId, String videoId) {
		ResultCode<VideoVo> result = videoRemoteService.getDetailById(showId, videoId);
		return convertVideoDTO(result.getRetval());
	}

	@Override
	public String getVideoUrlById(VideoPlayVo videoPlay) {
		if (videoPlay != null) {
			// 又拍云视频
			List<VideoDefinitionsVo> definitions = videoPlay.getDefinitions();
			if (CollectionUtils.isNotEmpty(definitions)) {
				VideoDefinitionsVo videoDefinitionsVo = definitions.get(definitions.size() - 1);
				return videoDefinitionsVo.getPlayurl();
			}
		}
		return null;
	}

	@Override
	public String getVideoCodeById(String showId, String videoId, Double width, Double height) {
		ResultCode<VideoVo> result = videoRemoteService.getDetailById(showId, videoId);
		String videoUrl = getVideoUrlById(result.getRetval().getVideoPlay());
		if (StringUtils.isEmpty(videoUrl)) {
			// CC视频
			return getVideoCodeFromCC(videoId, width, height);
		} else {
			return "<video id=\"" + result.getRetval().getVideoid() + "\" controls webkit-playsinline=\"\" x-webkit-airplay=\"allow\" poster=\"\" src=\"" + videoUrl + "\"></video>";
		}
	}

	private String getVideoCodeFromCC(String videoId, Double width, Double height) {
		Map<String, String> params = new HashMap<String, String>();
		if (width != null) {
			params.put("player_width", width.toString());
		}
		if (height != null) {
			params.put("player_height", height.toString());
		}
		params.put("auto_play", "false");

		ResultCode<String> result = ccRemoteService.getPlayCode(videoId, params);
		return result.getRetval();
	}

	@Override
	public VideoDTO editVideo(VideoDTO videoDTO) {
		VideoVo video = new VideoVo();
		video.setVideoid(videoDTO.getVideoId());
		video.setTitle(videoDTO.getVideoTitle());
		video.setVideoDesc(videoDTO.getVideoDesc());
		video.setVideono(videoDTO.getVideoNo());
		video.setCameraType(videoDTO.getCameraType());
		video.setShowid(videoDTO.getShowId());
		video.setPicurl(videoDTO.getVideoPicUrl());
		video.setVideoStatus("publish");
		video.setUpdownAngle(videoDTO.getUpdownAngle());
		video.setLeftRightAngle(videoDTO.getLeftRightAngle());
		// video.setSubtitle(subtitle);
		ResultCode<VideoVo> result = videoRemoteService.saveOrModify(video);
		return convertVideoDTO(result.getRetval());
	}

	@Override
	public void deleteVideo(String showId, String videoId) {
		videoRemoteService.removeById(showId, videoId);
	}

	@Override
	@SuppressWarnings("unchecked")
	public PaginationResultDTO<PicDTO> getPicList(DataTableCriterias criterias, String showId) {
		PaginationReqVo<StagePhotoReqVo> reqVo = fillPaginationInfo(criterias);
		reqVo.setShowid(showId);
		ResultCode<PaginationRespVo<StagePhotoRespVo>> serverResp = stagePhotoRemoteService.queryList(reqVo);
		List<PicDTO> resultList = (List<PicDTO>) CollectionUtils.collect(serverResp.getRetval().getResp(), new Transformer() {

			@Override
			public PicDTO transform(Object object) {
				return convertPicDTO((StagePhotoRespVo) object);
			}
		});
		PaginationResultDTO<PicDTO> result = new PaginationResultDTO<PicDTO>();
		result.setTotal(serverResp.getRetval().getTotal());
		result.setResultList(resultList);
		return result;
	}

	@Override
	public PicDTO getPicDetail(String showId, String picId) {
		ResultCode<StagePhotoRespVo> result = stagePhotoRemoteService.getDetailById(showId, picId);
		return convertPicDTO(result.getRetval());
	}

	@Override
	public void editPic(PicDTO picDTO) {
		StagePhotoReqVo reqVo = new StagePhotoReqVo();
		reqVo.setPhotoId(picDTO.getPicId());
		reqVo.setPicdesc(picDTO.getPicDesc());
		reqVo.setPicurl(picDTO.getPicUrl());
		stagePhotoRemoteService.saveOrModify(picDTO.getShowId(), reqVo);
	}

	@Override
	public void deletePic(String showId, String picId) {
		stagePhotoRemoteService.removeById(showId, picId);
	}

	@Override
	@SuppressWarnings("unchecked")
	public PaginationResultDTO<ActorDTO> getActorList(DataTableCriterias criterias, String showId) {
		PaginationReqVo<ActorQueryReqVo> reqVo = fillPaginationInfo(criterias);
		reqVo.setShowid(showId);
		ResultCode<PaginationRespVo<ActorVo>> serverResp = actorRemoteService.queryList(reqVo);
		List<ActorDTO> resultList = (List<ActorDTO>) CollectionUtils.collect(serverResp.getRetval().getResp(), new Transformer() {

			@Override
			public ActorDTO transform(Object object) {
				return convertActorDTO((ActorVo) object);
			}
		});
		PaginationResultDTO<ActorDTO> result = new PaginationResultDTO<ActorDTO>();
		result.setTotal(serverResp.getRetval().getTotal());
		result.setResultList(resultList);
		return result;
	}

	@Override
	public void deleteActor(String showId, String actorId) {
		actorRemoteService.removeById(actorId);
	}

	@Override
	public ActorDTO editActor(ActorDTO actorDTO) {
		ActorVo reqVo = new ActorVo();
		reqVo.setActorid(actorDTO.getActorId());
		reqVo.setActorName(actorDTO.getActorName());
		reqVo.setActorDesc(actorDTO.getActorDesc());
		reqVo.setContentHtml(actorDTO.getContentHtml());
		reqVo.setHeadPicture(actorDTO.getActorPicUrl());
		reqVo.setPeriod(actorDTO.getPeriod());
		reqVo.setPicInfo(actorDTO.getActorPosterUrl());
		reqVo.setShowid(actorDTO.getShowId());
		reqVo.setIsNotClick(actorDTO.getIsNotClick());
		ResultCode<ActorVo> result = actorRemoteService.saveOrModify(reqVo);
		return convertActorDTO(result.getRetval());
	}

	@Override
	public ActorDTO getActorDetail(String showId, String actorId) {
		ResultCode<ActorVo> result = actorRemoteService.getDetailById(showId, actorId);
		return convertActorDTO(result.getRetval());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VideoDTO> getVideoList(List<String> videoIdList) {
		ResultCode<List<VideoVo>> resp = videoRemoteService.queryByIds(videoIdList);
		List<VideoDTO> resultList = (List<VideoDTO>) CollectionUtils.collect(resp.getRetval(), new Transformer() {

			@Override
			public VideoDTO transform(Object object) {
				return convertVideoDTO((VideoVo) object);
			}
		});
		return resultList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PicDTO> getPicList(List<String> picIdList) {
		ResultCode<List<StagePhotoRespVo>> resp = stagePhotoRemoteService.queryByIds(picIdList);
		List<PicDTO> resultList = (List<PicDTO>) CollectionUtils.collect(resp.getRetval(), new Transformer() {

			@Override
			public PicDTO transform(Object object) {
				return convertPicDTO((StagePhotoRespVo) object);
			}
		});
		return resultList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ActorDTO> getActorList(List<String> actorIdList) {
		ResultCode<List<ActorVo>> resp = actorRemoteService.queryByIds(actorIdList);
		List<ActorDTO> resultList = (List<ActorDTO>) CollectionUtils.collect(resp.getRetval(), new Transformer() {

			@Override
			public ActorDTO transform(Object object) {
				return convertActorDTO((ActorVo) object);
			}
		});
		return resultList;
	}

	@Override
	public void relateResource(String showId, String orignResourceType, String orignResourceId, String targetResourceType, List<String> targetResourceIds) {
		sourceRelatedVoV2Service.resourceRelatedByResource(orignResourceType, orignResourceId, targetResourceType, targetResourceIds, showId);
	}

	@Override
	public List<String> getRelatedResourceIds(String showId, String orignResourceType, String orignResourceId, String targetResourceType) {
		ResultCode<List<String>> result = sourceRelatedVoV2Service.getResourceIds(orignResourceType, orignResourceId, targetResourceType, showId);
		return result.getRetval();
	}

	@Override
	@SuppressWarnings("unchecked")
	public PaginationResultDTO<ItemDTO> getItemList(DataTableCriterias criterias, String showId) {
		PaginationReqVo<GoodsVoResp> reqVo = fillPaginationInfo(criterias);
		reqVo.setShowid(showId);
		ResultCode<PaginationRespVo<GoodsVoResp>> serverResp = goodsVoService.goodsList(reqVo);
		List<ItemDTO> resultList = (List<ItemDTO>) CollectionUtils.collect(serverResp.getRetval().getResp(), new Transformer() {

			@Override
			public ItemDTO transform(Object object) {
				return convertItemDTO((GoodsVoResp) object);
			}
		});
		PaginationResultDTO<ItemDTO> result = new PaginationResultDTO<ItemDTO>();
		result.setTotal(serverResp.getRetval().getTotal());
		result.setResultList(resultList);
		return result;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<ItemDTO> getItemList(List<String> itemIdList) {
		ResultCode<List<GoodsVoResp>> result = goodsVoService.goodsList(itemIdList);
		List<ItemDTO> resultList = (List<ItemDTO>) CollectionUtils.collect(result.getRetval(), new Transformer() {

			@Override
			public ItemDTO transform(Object object) {
				return convertItemDTO((GoodsVoResp) object);
			}
		});
		return resultList;
	}

	@Override
	public void deleteItem(String showId, String itemId) {
		goodsVoService.removeGoods(itemId);
	}

	@Override
	public ItemDTO getItemDetail(String showId, String itemId) {
		ResultCode<GoodsVoResp> goodsDetail = goodsVoService.goodsDetail(itemId);
		return convertItemDTO(goodsDetail.getRetval());
	}

	@SuppressWarnings("unchecked")
	@Override
	public PaginationResultDTO<PrizeDTO> getPrizeList(DataTableCriterias criterias, String showId) {
		PaginationReqVo<AubePrizeV2Vo> reqVo = fillPaginationInfo(criterias);
		reqVo.setShowid(showId);;
		ResultCode<PaginationRespVo<AubePrizeV2Vo>> serverResp = prizeV2VoService.queryList(reqVo);
		List<PrizeDTO> resultList = (List<PrizeDTO>) CollectionUtils.collect(serverResp.getRetval().getResp(), new Transformer() {

			@Override
			public PrizeDTO transform(Object object) {
				return convertPrizeDTO((AubePrizeV2Vo) object);
			}
		});
		PaginationResultDTO<PrizeDTO> result = new PaginationResultDTO<PrizeDTO>();
		result.setTotal(serverResp.getRetval().getTotal());
		result.setResultList(resultList);
		return result;
	}
	
	@Override
	public void deletePrize(String prizeId) {
		prizeV2VoService.removeById(Long.parseLong(prizeId));
	}
	
	@Override
	public void editPrize(PrizeDTO prizeDTO) {
		AubePrizeV2Vo vo = new AubePrizeV2Vo();
		if(StringUtils.isNotEmpty(prizeDTO.getPrizeId())) {
			vo.setId(Long.parseLong(prizeDTO.getPrizeId()));
		}
		vo.setPicurl(prizeDTO.getPicUrl());
		vo.setPrizeDesc(prizeDTO.getPrizeDesc());
		vo.setPrizeName(prizeDTO.getPrizeName());
		vo.setPrizeStock(prizeDTO.getPrizeStock());
		vo.setRemainStock(prizeDTO.getRemainStock());
		vo.setShowid(Long.parseLong(prizeDTO.getShowId()));
		prizeV2VoService.saveOrModify(vo);
	}
	
	
	@Override
	public PrizeDTO getPrizeDetail(String prizeId) {
		ResultCode<AubePrizeV2Vo> result = prizeV2VoService.getDetailById(Long.parseLong(prizeId));
		return convertPrizeDTO(result.getRetval());
	}
	
	@Override
	public void addPrizeStock(String prizeId, Integer addStock) {
		prizeV2VoService.addPrizeStock(Long.parseLong(prizeId), addStock.longValue());
	}
	
	@Override
	public void editItem(ItemDTO itemDTO) {
		GoodsVoResp goodsVoResp = new GoodsVoResp();
		goodsVoResp.setGoodsid(itemDTO.getItemId());
		goodsVoResp.setGoodsURL(itemDTO.getBuyUrl());
		goodsVoResp.setMultiPicUrlList(itemDTO.getDetailPicUrl());
		goodsVoResp.setPicurl(itemDTO.getTagPicUrl());
		goodsVoResp.setPrice(itemDTO.getPrice());
		goodsVoResp.setShowid(itemDTO.getShowId());
		goodsVoResp.setTlDesc(itemDTO.getItemDesc());
		goodsVoResp.setTlTitle(itemDTO.getItemName());
		goodsVoResp.setType(itemDTO.getItemType());
		goodsVoService.saveOrModifyGoods(goodsVoResp);
	}

	private ActorDTO convertActorDTO(ActorVo actorVo) {
		ActorDTO dto = new ActorDTO();
		dto.setActorDesc(actorVo.getActorDesc());
		dto.setActorId(actorVo.getActorid());
		dto.setActorName(actorVo.getActorName());
		dto.setActorPicUrl(actorVo.getHeadPicture());
		dto.setShowId(actorVo.getShowid());
		dto.setContentHtml(actorVo.getContentHtml());
		dto.setActorPosterUrl(actorVo.getPicInfo());
		dto.setPeriod(actorVo.getPeriod());
		dto.setIsNotClick(actorVo.getIsNotClick());
		return dto;
	}

	private ItemDTO convertItemDTO(GoodsVoResp vo) {
		ItemDTO dto = new ItemDTO();
		dto.setBuyUrl(vo.getGoodsURL());
		dto.setDetailPicUrl(vo.getMultiPicUrlList());
		dto.setItemDesc(vo.getTlDesc());
		dto.setItemId(vo.getGoodsid());
		dto.setItemName(vo.getTlTitle());
		dto.setPrice(vo.getPrice());
		dto.setShowId(vo.getShowid());
		dto.setTagPicUrl(vo.getPicurl());
		dto.setItemType(vo.getType());
		return dto;
	}

	private PicDTO convertPicDTO(StagePhotoRespVo stagePhotoVo) {
		PicDTO dto = new PicDTO();
		dto.setPicId(stagePhotoVo.getPhotoId());
		dto.setPicUrl(stagePhotoVo.getPicurl());
		dto.setPicDesc(stagePhotoVo.getPicdesc());
		return dto;
	}

	private VideoDTO convertVideoDTO(VideoVo videoVo) {
		VideoDTO dto = new VideoDTO();
		dto.setShowId(videoVo.getShowid());
		dto.setVideoTitle(videoVo.getTitle());
		dto.setVideoDesc(videoVo.getVideoDesc());
		dto.setVideoNo(videoVo.getVideono());
		dto.setVideoId(videoVo.getVideoid());
		dto.setVideoPicUrl(videoVo.getPicurl());
		dto.setCameraType(videoVo.getCameraType());
		dto.setCcStatus(videoVo.getCcStatus());
		if (videoVo.getCcStatus() != null) {
			dto.setCcStatusDesc(CcStatus.getEnumByCode(videoVo.getCcStatus()).getMsg());
		}
		StringBuffer upyunStatusDesc = new StringBuffer();
		Map<String, String> upaiStatus = videoVo.getUpaiStatusDetail();
		if (upaiStatus != null) {
			for (String key : upaiStatus.keySet()) {
				upyunStatusDesc.append(key);
				upyunStatusDesc.append(": ");
				upyunStatusDesc.append(upaiStatus.get(key) + " ");
			}
		}
		dto.setUpyunStatus(videoVo.getUpaiStatus());
		dto.setUpyunStatusDesc(upyunStatusDesc.toString());
		dto.setVideoUrl(getVideoUrlById(videoVo.getVideoPlay()));
		dto.setUpdownAngle(videoVo.getUpdownAngle());
		dto.setLeftRightAngle(videoVo.getLeftRightAngle());
		return dto;
	}


	private PrizeDTO convertPrizeDTO(AubePrizeV2Vo aubePrizeV2Vo) {
		PrizeDTO dto = new PrizeDTO();
		dto.setPicUrl(aubePrizeV2Vo.getPicurl());
		dto.setPrizeDesc(aubePrizeV2Vo.getPrizeDesc());
		dto.setPrizeId(aubePrizeV2Vo.getId().toString());
		dto.setPrizeName(aubePrizeV2Vo.getPrizeName());
		dto.setPrizeStock(aubePrizeV2Vo.getPrizeStock());
		dto.setRemainStock(aubePrizeV2Vo.getRemainStock());
		dto.setShowId(aubePrizeV2Vo.getShowid().toString());
		return dto;
	}

}
